<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
        <title>Document</title>
        <style>
            #div1{width: 200px; height: 200px; background-color: red; position: absolute}
        </style>
        <script>
            window.onload = function(){
                var oDiv = document.getElementById("div1");
                oDiv.onmousedown = function(ev){
                    var e = ev || window.event;
                    // alert(e.offsetX + ", " + e.offsetY);
                    var offsetX = e.clientX - oDiv.offsetLeft;
                    var offsetY = e.clientY - oDiv.offsetTop;

                    document.onmousemove = function(ev){
                        var e = ev || window.event;
                        oDiv.style.left = e.clientX - offsetX + 'px';
                        oDiv.style.top = e.clientY - offsetY + 'px';
                    }
                }

                document.onmouseup = function(){
                    document.onmousemove = null;
                }
            }
        </script>
    </head>
    <body>
        <div id = 'div1'></div>
    </body>
</html>